package cn.edu.scau.dormitory_management_system.service.impl;

import cn.edu.scau.dormitory_management_system.constant.Consts;
import cn.edu.scau.dormitory_management_system.dao.UserRoleDao;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import cn.edu.scau.dormitory_management_system.entity.UserRoleEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.RoleDto;
import cn.edu.scau.dormitory_management_system.module.user.service.UserService;
import cn.edu.scau.dormitory_management_system.service.UserRoleService;
import cn.edu.scau.dormitory_management_system.util.MailUtils;
import cn.edu.scau.dormitory_management_system.util.TaleUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRoleEntity> implements UserRoleService {
    @Autowired(required = false)
    UserRoleDao userRoleDao;
    @Autowired(required = false)
    UserService userService;
    @Override
    public boolean select(int userId, int roleId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("role_id",roleId);
        List<UserRoleEntity> userRoleEntities = userRoleDao.selectByMap(map);
        return userRoleEntities.size() == 1;
    }

    @Override
    public int authorization(String[] roleIds, int userId,int createId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        userRoleDao.deleteByMap(map);
        int count = 0;
        for (int i = 1; i < roleIds.length; i++) {
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            userRoleEntity.setCreateBy(String.valueOf(createId));
            userRoleEntity.setRoleId(Integer.valueOf(roleIds[i]));
            userRoleEntity.setUserId(userId);
            userRoleDao.insert(userRoleEntity);
            count++;
        }
        return count == roleIds.length-1?1:0;
    }
    @Override
    public int register(UserEntity user){
        UserEntity userEntity = userService.queryByUserName(user.getUserName());
        if (userEntity == null){
            user.setIsAdmin(1);
            user.setRandomCode(UUID.randomUUID().toString().replace("-", ""));
            user.setPassword(TaleUtils.MD5encode(user.getPassword()+user.getEmail()));
            if (userService.insertUser(user)){
                return user.getUserId();
            }else {
                return -1;
            }
        }else {
            return -2;
        }
    }

    @Override
    public int update(UserEntity user) {
        user.setIsAdmin(1);
        user.setRandomCode(UUID.randomUUID().toString().replace("-", ""));
        user.setPassword(TaleUtils.MD5encode(user.getPassword()+user.getEmail()));
        if (userService.updateById(user)){
            return user.getUserId();
        }
        return -1;
    }

    @Override
    public RoleDto info(int userId) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        UserEntity one = userService.getOne(queryWrapper);
        List<Integer> roleIdList = userRoleDao.roleIdList(userId);
        RoleDto roleDto = new RoleDto();
        roleDto.setUser(one);
        roleDto.setRoleIdList(roleIdList);
        return roleDto;
    }

}
